<?php

namespace App\Service\Collect;


use QL\QueryList;

class Sina extends Base
{
    // TODO 新浪暂时没有数据 未验证

    protected $ctsDb;
    protected $toDb;


    // TODO 从手机端获取数据 暂未验证
    // 从新浪财经网站采集数据：手机端（采集和pc端的一样 因为采集页面会自动转跳到pc端）
    protected function sinaMobile($uid, $time, $weIds)
    {
        try{
            // 获取列表
            $url = 'https://stocknews.cj.sina.cn/stocknews/api/news/get?market=cn&symbol=sz300688&su2cu=1&page=1&num=30&fr=wap&mi=1&_=1697620780834&callback=jsonp2';
            // TODO 修改采集 获取文章
            $res   = $this->apiCurl()->request($url); // 采集返回的是 try 需要整理成json
            $start = strpos($res, '(');
            $end   = strpos($res, ');');
            $json  = substr($res, $start + 1, $end - $start - 1);
            $rst   = json_decode($json, true);
            $lists = $rst['result']['data'] ?? [];
            $today = strtotime(date('Y-m-d'));
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }

        // 采集详情
        // 采集和pc端的一样 因为页面会自动转跳到pc端
        foreach($lists as $key => $item){
            $_title = $item['title'];
            $_ctime = $item['ctime'];  // 时间
            $wapurl = $item['wapurl'];
            $_href  = $item['url'];
            // 判断链接是否已经采集
            if($is_file){
                $info = $this->isGatherFile($_href);
            } else{
                $info = $this->isGather($_href);
            }
            if($info){
                if(empty($info['sentiment'])){
                    $sentiment = $this->baiduNlp($info['text'] ?: $info['description']);
                    if($sentiment){
                        if($is_file){
                            $info['sentiment'] = $sentiment;
                            $this->updateFile($_href, $info);
                        } else{
                            $this->updateArticle($info['id'], ['sentiment' => $sentiment]);
                        }
                    }
                }

                $this->showMsg(' '.($key + 1).'：==> 已经采集文章：'.$_title);
                continue;
            }

            // 只采集今天的
            if($_ctime < $today){
                $this->showMsg(' '.($key + 1).'：==> 文章：不是今天的文章：'.$_title);
                continue;
            }

            // 如果没有标题
            if(empty($_title)){
                continue;
            }
            // 查询
            try{
                $this->showMsg(' '.($key + 1).'：==> 采集文章：'.$_title);
                $_ql = QueryList::get($_href);
            } catch(\Exception $e){
                return $this->return_false($e->getMessage());
            }

            // 如果有 p 标签
            $rule  = ['html' => ['', 'html'], 'text' => ['', 'text']];
            $list  = $_ql->range('#artibody p')->rules($rule)->queryData();
            $_text = '';
            $_desc = '';
            if($list){
                foreach($list as $_item){
                    // 这里不直接用 $_item['text']  避免里面有图片
                    $_rangeHtml = $this->removeHtml($_item['html']);
                    if(empty($_rangeHtml)){
                        continue;
                    }
                    if(empty($_desc) && (strpos($_rangeHtml, '记者') === false) && (strpos($_rangeHtml, '转自') === false) && (strpos($_rangeHtml, '|') === false) && (strpos($_rangeHtml, '来源') === false)){
                        $_desc = $_rangeHtml;
                    }
                    $_text .= '<p style="padding: 3px 0;">'.$_rangeHtml.'</p>';
                }
            } else{
                // 如果没有 p 标签
                // 这里不直接用 ->text()  避免里面有图片
                //$_desc = $_ql->find('#artibody')->text();
                $_html = $_ql->find('#artibody')->html();
                $_html = $this->removeHtml($_html);
                $_desc = $_html;
                if($_html){
                    $_text .= '<p style="padding: 3px 0;">'.$_html.'</p>';
                }
            }

            // 全匹配 精准匹配
            if((strpos($_desc, $this->keyword) === false) && strpos($_title, $this->keyword) === false){
                return $this->return_false('==> 文章：文章没有精准匹配到搜索的关键字！');
            }
            // 排除关键字 宋正兴
            if(($this->keyword == '董事长宋正') && (strpos($_desc, '宋正兴') !== false) || strpos($_title, '宋正兴') !== false){
                return $this->return_false('==> 文章：文章匹配到其他关键词！');
            }


            // echo chr(10).chr(10).chr(10);
            // var_dump($_title);
            // var_dump($_href);
            // var_dump($_ctime);
            // var_dump($today);
            // var_dump($_desc);
            //var_dump($_text);
            //var_dump($_html);
            $this->showMsg('      ==> 保存到数据库：'.$_title);
            $this->saveArticle($uid, $_href, $_title, $_desc, $_text, $time, $_author = '', $is_show = false);
            $this->showMsg('      ==> 发送到微信：'.var_export($weIds, 1));
            $this->toWechatGroup($_title, $_href, $weIds);
        }

        return true;
    }

    // 从新浪财经网站采集数据
    protected function vip($stockId, $uid, $time, $weIds)
    {
        // 获取列表
        try{
            $url   = 'https://vip.stock.finance.sina.com.cn/corp/go.php/vCB_AllNewsStock/symbol/sz300688.phtml';
            $url   = 'https://vip.stock.finance.sina.com.cn/corp/go.php/vCB_AllNewsStock/symbol/'.$stockId.'.phtml';
            $rule  = ['href' => ['', 'href'], 'title' => ['', 'text']];
            $lists = $this->getQLList($url, '#con02-7 table td .datelist', 'ul a', $rule);
            $today = strtotime(date('Y-m-d'));
            $today = strtotime(date('Y-m-d')) - 86400 * 2;
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }

        // 采集详情
        foreach($lists as $key => $item){
            $_title  = $item['title'];
            $_href   = $item['href'];
            $_author = '';
            // 判断链接是否已经采集
            if($is_file){
                $info = $this->isGatherFile($_href);
            } else{
                $info = $this->isGather($_href);
            }
            if($info){
                if(empty($info['sentiment'])){
                    $sentiment = $this->baiduNlp($info['text'] ?: $info['description']);
                    if($sentiment){
                        if($is_file){
                            $info['sentiment'] = $sentiment;
                            $this->updateFile($_href, $info);
                        } else{
                            $this->updateArticle($info['id'], ['sentiment' => $sentiment]);
                        }
                    }
                }

                $this->showMsg(' '.($key + 1).'：==> 已经采集文章：'.$_title);
                continue;
            }
            // 查询
            try{
                $_ql = QueryList::get($_href);
            } catch(\Exception $e){
                return $this->return_false($e->getMessage());
            }

            // 时间
            $_date1 = $_ql->find('.date')->text();
            $_date2 = $_ql->find('.news-date')->text();
            $_date  = $_date1 ?: $_date2;
            if(strpos($_date, '年') === false){
                $_date = date("Y年").$_date;
            }
            $_date  = str_replace('年', '-', $_date);
            $_date  = str_replace('月', '-', $_date);
            $_date  = str_replace('日', ' ', $_date);
            $_ctime = strtotime($_date);

            // 只采集今天的
            if($_ctime < $today){
                $this->showMsg(' '.($key + 1).'：==> 文章：不是今天的文章：'.$_title);
                continue;
            }

            // 如果没有标题
            if(empty($_title)){
                continue;
            }
            // 如果有 p 标签
            $this->showMsg(' '.($key + 1).'：==> 采集文章：'.$_title);
            $rule  = ['html' => ['', 'html'], 'text' => ['', 'text']];
            $list  = $_ql->range('#artibody p')->rules($rule)->queryData();
            $_text = '';
            $_desc = '';
            if($list){
                foreach($list as $_item){
                    // 这里不直接用 $_item['text']  避免里面有图片
                    $_rangeHtml = $this->removeHtml($_item['html']);
                    if(empty($_rangeHtml)){
                        continue;
                    }
                    if(empty($_desc) && (strpos($_rangeHtml, '记者') === false) && (strpos($_rangeHtml, '转自') === false) && (strpos($_rangeHtml, '|') === false) && (strpos($_rangeHtml, '来源') === false)){
                        $_desc = $_rangeHtml;
                    }
                    $_desc = $this->replaceDescribe($_desc);
                    $_text .= '<p style="padding: 3px 0;">'.$_rangeHtml.'</p>';
                }
            } else{
                // 如果没有 p 标签
                // 这里不直接用 ->text()  避免里面有图片
                //$_desc = $_ql->find('#artibody')->text();
                $_html = $_ql->find('#artibody')->html();
                $_html = $this->removeHtml($_html);
                $_desc = $this->replaceDescribe($_html);
                if($_html){
                    $_text .= '<p style="padding: 3px 0;">'.$_html.'</p>';
                }
            }
            // 全匹配 精准匹配
            if((strpos($_desc, $this->keyword) === false) && strpos($_title, $this->keyword) === false){
                return $this->return_false('==> 文章：文章没有精准匹配到搜索的关键字！');
            }

            // 组合参数用于调用
            // echo chr(10).chr(10).chr(10);
            // var_dump($_date);
            // var_dump($today);
            // var_dump($_html);
            // $_ctime  $time
            $_row = ['uid' => $uid, 'link' => $_href, 'title' => $_title, 'description' => $_desc, 'text' => $_text, 'time' => $time, 'author' => $_author, 'is_show' => true, 'to_channel' => 'dark', 'from_site' => 'sina',];
            $this->showMsg('      ==> 保存到数据库：'.$_title);
            $this->saveArticle($_row);
            $this->showMsg('      ==> 发送到微信：'.var_export($weIds, 1));
            $this->toWechatGroup($_title, $_href, $weIds);
        }

        return true;
    }

    // 从新浪财经：研报网站采集数据
    protected function finance($stockId, $uid, $time, $weIds)
    {
        // 获取列表
        try{
            $url   = 'https://stock.finance.sina.com.cn/stock/go.php/vReport_List/kind/search/index.phtml?symbol=300688&t1=all';
            $url   = 'https://stock.finance.sina.com.cn/stock/go.php/vReport_List/kind/search/index.phtml?symbol='.$stockId.'&t1=all';
            $rule  = ['html' => ['', 'html']];
            $lists = $this->getQLList($url, '.wrap ', '.main table tr', $rule);
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }

        $today = strtotime(date('Y-m-d'));
        $today = strtotime(date('Y-m-d')) - 86400 * 2;
        $_rule = ['html' => ['', 'html'], 'text' => ['', 'text'], 'link' => ['a', 'href']];
        foreach($lists as $key => $item){
            $_ql   = QueryList::html($item['html']);
            $_list = $_ql->range('td')->rules($_rule)->queryData();
            if(count($_list) < 6){
                continue;
            }
            // 组合数据
            $_item = [
                'index'  => $_list[0]['text'],
                'title'  => trim($_list[1]['text']),
                'href'   => trim($_list[1]['link']),
                'type'   => $_list[2]['text'],
                'date'   => $_list[3]['text'],
                'origin' => trim($_list[4]['text']),
                'author' => $_list[5]['text'],
            ];

            // 执行采集
            $_title  = $_item['title'];
            $_href   = $_item['href'];
            $_date   = $_item['date'];
            $_ctime  = strtotime($_date); // 时间
            $_author = '';
            //采集链接
            if(strpos($_href, 'http') === false){
                $_href = 'https:'.$_href;
            }

            // 判断链接是否已经采集
            if($is_file){
                $info = $this->isGatherFile($_href);
            } else{
                $info = $this->isGather($_href);
            }
            if($info){
                if(empty($info['sentiment'])){
                    $sentiment = $this->baiduNlp($info['text'] ?: $info['description']);
                    if($sentiment){
                        if($is_file){
                            $info['sentiment'] = $sentiment;
                            $this->updateFile($_href, $info);
                        } else{
                            $this->updateArticle($info['id'], ['sentiment' => $sentiment]);
                        }
                    }
                }

                $this->showMsg(' '.($key + 1).'：==> 已经采集文章：'.$_title);
                continue;
            }
            // 只采集今天的
            if($_ctime < $today){
                $this->showMsg(' '.($key + 1).'：==> 文章：不是今天的文章：'.$_title);
                continue;
            }

            // 如果没有标题
            if(empty($_title)){
                continue;
            }

            // 查询
            try{
                $_ql = QueryList::get($_href);
            } catch(\Exception $e){
                return $this->return_false($e->getMessage());
            }

            // 如果有 p 标签
            $this->showMsg(' '.($key + 1).'：==> 采集文章：'.$_title);
            $rule  = ['html' => ['', 'html'], 'text' => ['', 'text']];
            $list  = $_ql->range('.content .blk_container p')->rules($rule)->queryData();
            $_text = '';
            $_desc = '';
            if($list){
                foreach($list as $_item){
                    // 这里不直接用 $_item['text']  避免里面有图片
                    $_rangeHtml = $this->removeHtml($_item['html']);
                    if(empty($_rangeHtml)){
                        continue;
                    }
                    if(empty($_desc) && (strpos($_rangeHtml, '记者') === false) && (strpos($_rangeHtml, '转自') === false) && (strpos($_rangeHtml, '|') === false) && (strpos($_rangeHtml, '来源') === false)){
                        $_desc = $_rangeHtml;
                    }
                    $_desc = $this->replaceDescribe($_desc);
                    $_text .= '<p style="padding: 3px 0;">'.$_rangeHtml.'</p>';
                }
            } else{
                // 如果没有 p 标签
                // 这里不直接用 ->text()  避免里面有图片
                //$_desc = $_ql->find('#artibody')->text();
                $_html = $_ql->find('.content .blk_container')->html();
                $_html = $this->removeHtml($_html);
                $_desc = $this->replaceDescribe($_html);
                if($_html){
                    $_text .= '<p style="padding: 3px 0;">'.$_html.'</p>';
                }
            }

            // 全匹配 精准匹配
            if((strpos($_desc, $this->keyword) === false) && strpos($_title, $this->keyword) === false){
                return $this->return_false('==> 文章：文章没有精准匹配到搜索的关键字！');
            }

            // echo chr(10).chr(10).chr(10);
            // var_dump($_date);
            // var_dump($today);
            //var_dump($_html);
            $_row = ['uid' => $uid, 'link' => $_href, 'title' => $_title, 'description' => $_desc, 'text' => $_text, 'time' => $time, 'author' => $_author, 'is_show' => true, 'to_channel' => 'dark', 'from_site' => 'sina_stock',];
            $this->showMsg('      ==> 保存到数据库：'.$_title);
            $this->saveArticle($_row);
            $this->showMsg('      ==> 发送到微信：'.var_export($weIds, 1));
            $this->toWechatGroup($_title, $_href, $weIds);
        }

        return true;
    }


}

